<?php
/**
 * 包名：BaseController
 * 创建时间：2019.6.28
 * 创建者博客： http://blog.csdn.net/xh870189248
 * 创建者GitHub： https://github.com/xuhongv
 * 创建者：徐宏
 * 描述： TODO
 */

namespace app\api\controller\oauth;


use app\common\device\DeviceCenter;
use think\Controller;

class BaseController extends Controller
{


    //各大云平台的返回统一状态码
    public $cloudsCodes_control_ok = 0; //控制成功
    public $cloudsCodes_control_offline = 1; //控制失败，设备离线
    public $cloudsCodes_control_not_exit = 2; //控制失败，设备在数据库未找到！
    public $cloudsCodes_control_error_server = 3; //控制失败，服务器内部异常！
    public $cloudsCodes_control_proper_not_support = 4; //控制失败，暂不支持此属性控制！

    public $cloudsMine = null; //私有协议

    protected function _initialize()
    {
        parent::_initialize(); // TODO: Change the autogenerated stub
        $this->cloudsMine = config('devicesAttr.MineName');
    }

    /**
     * @param string $controlType
     * @param string $messageId
     * @param string $actionName
     * @param string $attribute
     * @param string $deviceId
     * @param string $deviceType
     * @param $value
     * @param $payLoadVersion
     * @return int|string
     */
    public function reposeToDevice($controlType , $device , $actionName = ''
        , $attribute = '', $deviceId = '', $value, $payLoadVersion)
    {
        //构造数据发送到emq，发送到 ESP8266等设备；
        $arry = [
            "header" => [
                "name" => $actionName,
                "namespace" => $controlType,
                "payLoadVersion" => $payLoadVersion,
            ],
            "payload" => [
                "attribute" =>$attribute,
                "deviceId" => $deviceId,
                "deviceType" => $device->type,
                "value" => $value,
            ]
        ];

        $data = [
            "topic" => DeviceCenter::getDeviceSubMqttTopic($device['type'], $device['mac']),
            "qos" => 1,
            "retain" => false,
            "client_id" => "xuhongPhpClient",
            'username' => "xuhongPhpClient",
            "payload" => json_encode($arry),
        ];
        //utilsSaveLogs('send Mqtt :  ' . json_encode($data), 1);
        $result = post2Emq(config('url_mqtt'), $data);
        //utilsSaveLogs('send Mqtt result:  ' . json_encode($result), 1);
        return true;
    }

}